﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CSharpPro._0930;

public class User
{
    private  List<User> users = new List<User>();
    public string Username { get; set; }
    public string Password { get; set; }

    public User() { }

    public User(string username, string password)
    {
        Username = username;
        Password = password;
    }
    
    public void ShowMainMenu()
    {
        while (true)
        {
            Console.WriteLine("\n========== 主菜单 ==========");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.WriteLine("============================");
            Console.Write("请选择功能（输入数字1-3）：");

            string choiceInput = Console.ReadLine();
            
            if (!int.TryParse(choiceInput, out int choice))
            {
                Console.WriteLine("请输入有效数字！");
                continue;
            }

            switch (choice)
            {
                case 1:
                    Login();
                    break;
                case 2:
                    Register();
                    break;
                case 3:
                    Console.WriteLine("感谢使用，再见！");
                    return;
                default:
                    Console.WriteLine("无效选择，请输入1-3之间的数字！");
                    break;
            }
        }
    }
    
    public void Register()
    {
        Console.WriteLine("\n========== 用户注册 ==========");
        
        string username;
        while (true)
        {
            Console.Write("请输入用户名（3-15个字符，只能包含字母、数字、下划线）：");
            username = Console.ReadLine();
            
            if (string.IsNullOrWhiteSpace(username))
            {
                Console.WriteLine("用户名不可为空！");
                continue;
            }
            
            if (!IsValidUsername(username))
            {
                Console.WriteLine("用户名格式不正确！");
                continue;
            }
            
            if (IsUsernameExists(username))
            {
                Console.WriteLine("用户名已存在，请选择其他用户名！");
                continue;
            }
            
            break;
        }
        
        string password;
        while (true)
        {
            Console.Write("请输入密码（6-20个字符，必须包含至少1个大写字母、1个小写字母和1个数字）：");
            password = Console.ReadLine();
            
            if (string.IsNullOrWhiteSpace(password))
            {
                Console.WriteLine("密码不可为空！");
                continue;
            }
            
            if (!IsValidPassword(password))
            {
                Console.WriteLine("密码格式不正确！");
                continue;
            }
            
            break;
        }
        
        while (true)
        {
            Console.Write("请再次输入密码以确认：");
            string confirmPassword = Console.ReadLine();
            
            if (password != confirmPassword)
            {
                Console.WriteLine("两次输入的密码不一致，请重新输入！");
                continue;
            }
            
            break;
        }
        
        users.Add(new User(username, password));
        Console.WriteLine("注册成功！已自动返回主菜单。");
    }
    
    public void Login()
    {
        Console.WriteLine("\n========== 用户登录 ==========");
        
        Console.Write("请输入用户名：");
        string username = Console.ReadLine();
        
        if (string.IsNullOrWhiteSpace(username))
        {
            Console.WriteLine("用户名不可为空！");
            return;
        }
        
        User user = users.Find(u => u.Username == username);
        if (user == null)
        {
            Console.WriteLine("用户名不存在！");
            return;
        }
        
        Console.Write("请输入密码：");
        string password = Console.ReadLine();
        
        if (string.IsNullOrWhiteSpace(password))
        {
            Console.WriteLine("密码不可为空！");
            return;
        }
        
        if (user.Password != password)
        {
            Console.WriteLine("密码错误！");
            return;
        }
        
        Console.WriteLine($"欢迎，{username}！登录成功。");
        ShowSystemHomepage();
    }

    private void ShowSystemHomepage()
    {
        Console.WriteLine("\n========== 系统首页 ==========");
        Console.WriteLine("这里是系统首页，您可以进行各种操作...");
        Console.WriteLine("按任意键返回主菜单...");
        Console.ReadKey();
    }

    private bool IsValidUsername(string username)
    {
       
        if (username.Length < 3 || username.Length > 15)
            return false;
        
       
        Regex regex = new Regex(@"^[a-zA-Z0-9_]+$");
        return regex.IsMatch(username);
    }

   
    private bool IsValidPassword(string password)
    {
       
        if (password.Length < 6 || password.Length > 20)
            return false;
        bool hasUpper = false, hasLower = false, hasDigit = false;
        foreach (char c in password)
        {
            if (char.IsUpper(c)) hasUpper = true;
            else if (char.IsLower(c)) hasLower = true;
            else if (char.IsDigit(c)) hasDigit = true;
        }
        
        return hasUpper && hasLower && hasDigit;
    }
    private bool IsUsernameExists(string username)
    {
        return users.Exists(u => u.Username == username);
    }
}